//
//  ZHDrawerMenuController.m
//  ZHS_UCDemo
//
//  Created by Emrys on 2016/11/10.
//  Copyright © 2016年 Emrys. All rights reserved.
//

#import "ZHDrawerMenuController.h"

#import "ZHNavigationController.h"

#import "ZHLoginViewController.h"

#import "ZHTools.h"

#import "ZHImageView.h"
#import "ZHTableView.h"

#import "ZHDrawerMenuInfo.h"

#import "ZHDrawerMenuHeaderView.h"
#import "ZHDrawerMenuModel.h"
#import "ZHDrawerMenuCell.h"

static const NSString *kDrawerMenuPlist = @"DrawerMenuDataSource";
static const NSString *kLeader_0 = @"leader_0";
static const NSString *kLeader_1 = @"leader_1";

static const NSString *kMenuBackgroundImage = @"backgroundImage";
static const NSString *kMenuDataSource = @"menuDataSource";

@interface ZHDrawerMenuController ()<UITableViewDelegate, UITableViewDataSource>

@property(nonatomic, assign) NSInteger leaderSymbol;

@property (weak, nonatomic) IBOutlet ZHImageView *menuBackgroundImageView;
@property (weak, nonatomic) IBOutlet ZHTableView *menuTableView;

@property(nonatomic, strong) ZHDrawerMenuInfo *drawerMenuInfo;
@property(nonatomic, strong) NSArray *menuDataSource;

@end

@implementation ZHDrawerMenuController

+ (instancetype)drawerMenuControllerWithLeaderSymbol:(NSInteger)leaderSymbol {
    
    ZHDrawerMenuController *drawerMenuVC = [self viewControllerFromStoryboard];
    
    if (drawerMenuVC) {
        
        drawerMenuVC.leaderSymbol = leaderSymbol;
        
        return drawerMenuVC;
    }
    return nil;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setMenu];
    
}

#pragma mark - set
- (void)setMenu {
    
    self.menuTableView.delegate = self;
    self.menuTableView.dataSource = self;
    
    [self.menuTableView registerNib:[UINib nibWithNibName:NSStringFromClass([ZHDrawerMenuCell class])
                                                   bundle:nil]
             forCellReuseIdentifier:(NSString *)kZHDrawerMenuCell];
        
    self.menuTableView.bounces = NO;
    
    self.menuTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

#pragma mark - active
- (void)signOut {
    
    ZHLoginViewController *loginViewController = [ZHLoginViewController viewControllerFromStoryboard];
    
    [UIApplication sharedApplication].keyWindow.rootViewController = loginViewController;
    
}

- (NSDictionary *)loadMenuInfo {
    NSString *filePath = [[NSBundle mainBundle] pathForResource:(NSString *)kDrawerMenuPlist
                                                         ofType:(NSString *)kPlistType];
    
    NSDictionary *rootDictionart = [ZHTools dictionaryWithFile:filePath];
    
    switch (self.leaderSymbol) {
            case 0:
            return [rootDictionart valueForKey:(NSString *)kLeader_0];
            break;
            case 1:
            return [rootDictionart valueForKey:(NSString *)kLeader_1];
            break;
        default:
            return nil;
            break;
    }
}

#pragma mark - 
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ZHDrawerMenuModel *drawerMenuModel = self.menuDataSource[indexPath.row];
    
    if (drawerMenuModel.targetController.length > 0) {
        ZHViewController *targetCtl = [ZHViewController viewControllerFromStoryboardWithClassName:drawerMenuModel.targetController];
        
        ZHNavigationController *targetNavCtl = [[ZHNavigationController alloc] initWithRootViewController:targetCtl];
        
        targetCtl.title = drawerMenuModel.menuTitle;
        
        [self.drawerController setCenterViewController:targetNavCtl
                                    withCloseAnimation:YES
                                            completion:nil];
    }
    if (drawerMenuModel.selector.length > 0
        || [self respondsToSelector:NSSelectorFromString(drawerMenuModel.selector)]) {
        
        SuppressPerformSelectorLeakWarning(
        [self performSelector:NSSelectorFromString(drawerMenuModel.selector)]
        );
    }
    
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.menuDataSource.count;
}
- (ZHDrawerMenuCell *)tableView:(ZHTableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ZHDrawerMenuCell *cell = [tableView dequeueReusableCellWithIdentifier:(NSString *)kZHDrawerMenuCell forIndexPath:indexPath];
    
    cell.menuModel = self.menuDataSource[indexPath.row];
    
    return cell;
}

#pragma mark -
#pragma mark - property
- (NSArray *)menuDataSource {
    if (nil == _menuDataSource) {
        
        _menuDataSource = self.drawerMenuInfo.menuDataSource;
    }
    return _menuDataSource;
}

- (ZHDrawerMenuInfo *)drawerMenuInfo {
    if (nil == _drawerMenuInfo) {
        
        NSDictionary *menuInfoDict = [self loadMenuInfo];
        
        if (menuInfoDict) {
            
            _drawerMenuInfo = [ZHDrawerMenuInfo makeModelWithObject:menuInfoDict];
            
            ZHDrawerMenuHeaderView *menuHeader = [ZHDrawerMenuHeaderView drawerMenuHeaderWithLeaderSymbol:self.leaderSymbol];
            
            self.menuTableView.tableHeaderView = menuHeader;
            
            if (_drawerMenuInfo) {
                
                menuHeader.logoImageName = _drawerMenuInfo.menuHeaderLogoImage;
                
                self.menuBackgroundImageView.image = [UIImage imageNamed:_drawerMenuInfo.backgroundImage];
            }
        }
    }
    return _drawerMenuInfo;
}
@end
